<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #editor {
            height: 500px;
            border: solid 1px red;
        }
        /* 数学公式|不显示问题 */
        line {
            stroke-width: 1px;
        }
    </style>
</head>

<body>
    <div id="editor" contenteditable="true"></div>
    <div id="rtfAppend" class="hidden-elem"></div>

    <script src="./dist/WMFJS.bundle.min.js"></script>
    <script src="./dist/EMFJS.bundle.min.js"></script>
    <script src="./dist/RTFJS.bundle.js"></script>
    <script src="./dist/dom-to-image.min.js"></script>
    <script src="./dist/spark-md5.min.js"></script>
    <script src="./resources/utils.js"></script>

    <script>
        // 监听粘贴功能
        document.getElementById('editor').addEventListener('paste', function (evt) {
            evt.preventDefault();
            const clipItems = evt.clipboardData.items|| window.clipboardData.items;
			
			//const clipItems = evt.dataTransfer.items;
			
			//console.log( "clipboardData==>",evt.clipboardData)
			//console.log( "====plain==>",evt.clipboardData.getData('text/plain'))
			//console.log( "====html==>",evt.clipboardData.getData('text/html'))
			//console.log( "====rtf==>",evt.clipboardData.getData('text/rtf'))
		
			
			
            if (clipItems && clipItems.length > 0) {
                // 1、获取所有需要转换的元rtf
                const rtfPromiseArr = [];
                for(item of clipItems) {
					console.log(item)
                    item.type == 'text/rtf' && rtfPromiseArr.push(getRtf(item));
                }
                // 2、rtf 转换为 html元素
                Promise.all(rtfPromiseArr).then(data => {
                    new RtfConverter({
                        data: data,
                        onSuccess: function(res) {
                            if (res) {
                                document.querySelector('#rtfAppend').innerHTML = res.element.innerHTML;
                                console.log('======={element: "", file: {}==========');
                                console.log(res)
                                // 2.1、调用后台批量上传
                                // 2.2、上传成功后，替换img路径为后台返回路径
                                // 2.3、将parentWrap的innerHTML添加到光标处
                            } else {
                                console.error('[自定义Error]: 没有要转换的rtf');
                            }
                        }
                    });
                })
            }
        });
        function getRtf(rtfItem) { // 获取rtf串
            return new Promise((resolve, reject) => {
			    
                rtfItem.getAsString((rtfStr) => { // 获取到rtf字符串
                     rtfStr ? resolve(rtfStr) : reject();
					//console.log('rtf string==>\n',rtfStr)
                })
            });

        }
    </script>
</body>

</html>